package com.itwn.comic.web;

import com.github.pagehelper.PageInfo;
import com.itwn.comic.entity.Page;
import com.itwn.comic.reult.ResponseResult;
import com.itwn.comic.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("page")
public class PageController {

    @Value("${imgPath}")
    String imgPath;

    @Autowired
    PageService pageService;

    @GetMapping("/list")
    public ResponseResult<PageInfo<Page>> list(@RequestParam(value = "size",defaultValue = "5")Integer size,
                                               @RequestParam(value = "now",defaultValue = "1")Integer now,
                                               Integer id){
        ResponseResult<PageInfo<Page>> result = new ResponseResult<>();
        try {
            PageInfo<Page> pageInfo = pageService.queryPageBySectionId(size, now,id);
            if (pageInfo != null) {
                result.setCode(200);
                result.setMsg("查询成功");
                result.setData(pageInfo);
            } else {
                result.setCode(201);
                result.setMsg("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败");
        }
        return result;
    }
    @PostMapping("/add")
    public ResponseResult<Void> add(@RequestBody Page page){
        ResponseResult<Void> result = new ResponseResult<>();
        try{
            int i = pageService.addPage(page);
            if(i != 0){
                result.setCode(200);
                result.setMsg("添加成功");
            }else {
                result.setCode(201);
                result.setMsg("添加失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败");
        }
        return  result;
    }
    @PutMapping("/modify")
    public ResponseResult<Void> modify(@RequestBody Page page){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            int i = pageService.modifyPage(page);
            if(i != 0){
                result.setCode(200);
                result.setMsg("修改成功");
            }else {
                result.setCode(201);
                result.setMsg("修改失败");
            }
        }catch (Exception e){
            result.setCode(201);
            result.setMsg("修改失败");
        }
        return result;
    }
    @DeleteMapping("/del/{id}")
    public ResponseResult<Void> del(@PathVariable("id")int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            int i = pageService.removePageById(id);
            if(i != 0){
                result.setCode(200);
                result.setMsg("删除成功");
            }else {
                result.setCode(201);
                result.setMsg("删除失败");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败");
        }
        return  result;
    }
    @RequestMapping("/upload")
    public ResponseResult<String> upload(@RequestParam(value ="headerImg") MultipartFile uploadFile, Integer sid){
        System.out.println(uploadFile.getOriginalFilename());
        String filename = uploadFile.getOriginalFilename();
        int rid = pageService.queryRidBySid(sid);
        // 重新获取名字，保证不重复
        System.out.println(sid+"==========");
        System.out.println(rid+"==========");
        // E:/imgs/resource/id数字/comic2858292783742947.jpg
        File file = new File(imgPath + "resource/"+rid+"/"+sid+"/"+filename);

        System.out.println(imgPath + "resource/"+rid+"/"+sid+"/"+filename);
        if(!file.exists()){
            file.mkdirs();
            System.out.println(file.mkdirs());
        }

        ResponseResult<String> result = new ResponseResult<>();
        try {
            uploadFile.transferTo(file);
            result.setCode(200);
            result.setMsg("上传成功");
            result.setData(filename);
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("上传失败");
        }
        return result;
    }
}
